/*
 * @Description:
 * @version: 1.0.0
 * @Author: YinJianFeng
 * @Date: 2024-03-29 14:48:02
 */
import {
  cadAppLog,
  CMEVENTS,
  Constraint,
  contextMenuManager,
  designTreeMenuManager,
  DocumentUtils,
  IEnvironment,
} from '@amcax/base';
import { ctx2, docMgr, userEvents } from '@app-cad/module';
import { CadEnvironments } from '@app-cad-env/index';
import EnvSwitch from '@app-cad/common/components/EnvSwitch';
import { eventCenterManager } from '@app-cad/common/events/EventCenterManager';
import { exitSketcherMethod } from '@app-cad/common/utils';
import { cmdMgr } from '@app-cad/manager/CmdManager';
import { nextmeshEventCenter } from '../nextmesh/common/events/EventCenter';
import './common.scss';
import { defaultEventCenter } from '@app-cad/common/events/DefaultEventCenter';
import { sketchEventCenter } from './events/SketchEventCenter';
import { AssistLayer, CommandBar, RightNavigation, ToolBar } from './widgets';
import { DimContextMenu } from './widgets/ContextMenu/DimContextMenu';
import { SketchContextMenu } from './widgets/ContextMenu/SketchContextMenu';
import { LeftPanel } from './widgets/LeftPanel';
import { DefaultContextMenu } from './widgets/ContextMenu/DefaultContextMenu';
import { DimRightMenu } from './widgets/ContextMenu/DimRightMenu';
import { ConstraintContextMenu } from './widgets/ContextMenu/ConstraintContextMenu';
import { constraintIconRenderer } from '@app-cad/common/utils/sketchTools/ConstraintIconRenderer';
import { CircleBar } from '@app-cad/common/widgets/CircleBar';

const ReferenceIds = ['ORIGIN', 'XOY', 'XOZ', 'YOZ'];
const setReferenceVisibility = (visibility: boolean) => {
  ReferenceIds.forEach((id) => {
    docMgr.setLocalReferenceVisibility(id, visibility);
  });
};

const constraintDeleteHandler = (
  constraint: Constraint & { curveId: string },
) => {
  const icurve = constraintIconRenderer.renderDataByICurve.get(
    constraint.curveId,
  );
  constraintIconRenderer.removeConstraint(
    { ...constraint, parent: icurve.icurve },
    true,
  );
};

export const nextCadEnvironment: IEnvironment = {
  id: CadEnvironments.NextCAD,
  label: '设计',
  index: 0,
  showViewCube: true,
  init: () => {
    cadAppLog('Env Init NEXTCAD');
  },
  load: async function (params?: any) {
    if (!designTreeMenuManager.getById(DefaultContextMenu.id)) {
      designTreeMenuManager.register(DefaultContextMenu);
    }
    cadAppLog('Env Load NEXTCAD', params);
    userEvents.on(CMEVENTS.MODEL_RIGHT_CLICK, (event: PointerEvent) => {
      setTimeout(() => {
        contextMenuManager.show(event, SketchContextMenu.id);
        contextMenuManager.show(event, DimContextMenu.id);
        contextMenuManager.show(event, DimRightMenu.id);
        contextMenuManager.show(event, ConstraintContextMenu.id);
      });
    });

    userEvents.on(CMEVENTS.CONSTRAINT_DELETED, constraintDeleteHandler);

    cmdMgr.updateUndoSize();

    eventCenterManager
      .addEventCenter(defaultEventCenter())
      .addEventCenter(sketchEventCenter())
      .addEventCenter(nextmeshEventCenter())
      .switchTo(defaultEventCenter().id);

    setReferenceVisibility(true);

    setTimeout(() => {
      ctx2.viewer.fitObjectView();
    }, 200);

    cadAppLog('loadNextCAD');
  },
  unload: async () => {
    cadAppLog('Env UnLoad NEXTCAD');

    const sketchId = sketchEventCenter().getSketchInfo()?.sketchId;

    if (sketchId) {
      await exitSketcherMethod(sketchId);
    }

    setReferenceVisibility(false);

    userEvents.off(CMEVENTS.CONSTRAINT_DELETED, constraintDeleteHandler);
  },
  commandBar: (style: string = 'down') => {
    return DocumentUtils.isReadOnly() ? null : <CommandBar style={style} />;
  },
  contextMenus: () => {
    return DocumentUtils.isReadOnly()
      ? []
      : [
          SketchContextMenu,
          DimContextMenu,
          DimRightMenu,
          ConstraintContextMenu,
        ];
  },
  toolBar: () => {
    return DocumentUtils.isReadOnly() ? null : <ToolBar />;
  },
  leftTopNavBar: () => <EnvSwitch />,
  leftPanel: () => {
    return DocumentUtils.isReadOnly() ? null : <LeftPanel />;
  },
  rightNavigation: () => {
    return DocumentUtils.isReadOnly() ? null : <RightNavigation />;
  },
  actionBar: () => {
    return null;
  },
  assistLayer: () => {
    return <AssistLayer />;
  },
  circleBar: () => {
    return <CircleBar environment={'part_env'} />;
  },
};
// export default featureEnvironment;
